from django.shortcuts import render
from django.views.generic import View
from collections import OrderedDict

from contents.models import ContentCategory
from contents import utils


# Create your views here.


class IndexView(View):

    def get(self, request):
        categories = utils.get_categories()
        # 获取所有广告数据
        # 查询所有的广告类别
        contents = OrderedDict()
        content_categoryes = ContentCategory.objects.all()
        for content_category in content_categoryes:
            # 使用广告类别查询出所对应的广告类别
            contents[content_category.key] = content_category.content_set.filter(status=True).order_by('sequence')

        # 构造上下文
        context = {
            'categories': categories,
            'contents': contents,
        }

        return render(request, 'index.html', context)